package com.ruoyi.business.controller;

import com.ruoyi.business.service.IWsdCanteensService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.dto.WsdCanteensDTO;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.entity.WsdCanteens;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

import static com.ruoyi.common.utils.SecurityUtils.getLoginUser;

/**
 * 饭堂信息Controller
 *
 * @author ruoyi
 * @date 2024-04-15
 */
@RestController
@RequestMapping("wsd_canteens")
public class WsdCanteensController extends BaseController {

    @Autowired
    private IWsdCanteensService wsdCanteensService;


    /**
     * 查询饭堂信息列表
     */
    @PreAuthorize("@ss.hasPermi('business:canteens:list')")
    @GetMapping("/list")
    public TableDataInfo list(WsdCanteens wsdCanteens) {
        SysUser user = getLoginUser().getUser();
        if (!user.isAdmin()){
            Long schoolId = user.getSchoolId();
            wsdCanteens.setSchoolId(schoolId);
        }
        startPage();
        List<WsdCanteens> list = wsdCanteensService.selectWsdCanteensList(wsdCanteens);
        return getDataTable(list);
    }

    /**
     * 导出饭堂信息列表
     */
    @PreAuthorize("@ss.hasPermi('business:canteens:export')")
    @Log(title = "饭堂信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WsdCanteens wsdCanteens)
    {
        List<WsdCanteens> list = wsdCanteensService.selectWsdCanteensList(wsdCanteens);
        ExcelUtil<WsdCanteens> util = new ExcelUtil<WsdCanteens>(WsdCanteens.class);
        util.exportExcel(response, list, "饭堂信息数据");
    }

    /**
     * 获取饭堂信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:canteens:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(wsdCanteensService.selectWsdCanteensById(id));
    }

    /**
     * 新增饭堂信息
     */
    @PreAuthorize("@ss.hasPermi('business:canteens:add')")
    @Log(title = "饭堂信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WsdCanteensDTO wsdCanteensDTO) {
        SysUser user = getLoginUser().getUser();
        Long schoolId = user.getSchoolId();
        wsdCanteensDTO.setSchoolId(schoolId);
        return toAjax(wsdCanteensService.insertWsdCanteens(wsdCanteensDTO));
    }

    /**
     * 修改饭堂信息
     */
    @PreAuthorize("@ss.hasPermi('business:canteens:edit')")
    @Log(title = "饭堂信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WsdCanteensDTO wsdCanteensDTO) {
        return toAjax(wsdCanteensService.updateWsdCanteens(wsdCanteensDTO));
    }

    /**
     * 删除饭堂信息
     */
    @PreAuthorize("@ss.hasPermi('business:canteens:remove')")
    @Log(title = "饭堂信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(wsdCanteensService.deleteWsdCanteensByIds(ids));
    }
}
